package xiaoqi.springwebsockt.handler;

import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Component;
import xiaoqi.springwebsockt.message.SendResponse;
import xiaoqi.springwebsockt.message.SendToOneRequest;
import xiaoqi.springwebsockt.message.SendToUserRequest;
import xiaoqi.springwebsockt.util.WebSocketUtil;

import javax.websocket.Session;

@Component
public class SendToOneRequestHandler implements MessageHandler<SendToOneRequest> {
    @Override
    public void execute(Session session, SendToOneRequest message) {
        SendResponse sendResponse = new SendResponse().setMsgId(message.getMsgId()).setCode(0);
        WebSocketUtil.send(session, SendResponse.TYPE, sendResponse);

        SendToUserRequest sendToUserRequest = new SendToUserRequest()
                .setMsgId(message.getMsgId()).setContent(message.getContent());
        WebSocketUtil.send(message.getToUser(), SendToOneRequest.TYPE, message);
    }

    @Override
    public String getType() {
        return SendToOneRequest.TYPE;
    }
}
